//
//  ERHomeViewController.m
//  ExchangeRate
//
//  Created by 方义强 on 15/10/22.
//  Copyright © 2015年 方义强. All rights reserved.
//

#import "ERHomeViewController.h"
#import "ERHomeTableViewCell.h"
#import "ERCountryListViewController.h"
#import "ERCaculatorView.h"
#import "AFHTTPRequestOperation.h"
#import "ERResourceModel.h"
#import "UITableView+Wave.h"
#import "ERCountryJsonModel.h"
#import "ERToolsMainViewController.h"




#define kReuseIdentifier @"ExchangeRate"

typedef NS_ENUM(NSInteger, CaculateType)
{
    NSCaculateTypeAddition = 1,   // 加法
    NSCaculateTypeSubtraction,    // 减法
    NSCaculateTypeMultiplication, // 乘法
    NSCaculateTypeDivision        // 除法
};

@interface ERHomeViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) ERCaculatorView *caculatorView;
@property (nonatomic, strong) ERCountryListViewController *countryListVC;
@property (nonatomic, strong) UIVisualEffectView *effectView;

@property (nonatomic, strong) NSMutableArray *dataArr;  // 解析完所有数据数组
@property (nonatomic, strong) NSMutableArray *dataCellArr;
@property (nonatomic, strong) NSMutableArray *dataShowArr;  // 显示的model数组 4个
@property (nonatomic, strong) NSMutableArray *dataShowSimArr; // 显示简称
@property (nonatomic, strong) NSString *resultString;
@property (nonatomic, strong) NSString *caculatorString2;
@property (nonatomic, strong) NSString *caculatorString;
@property (nonatomic, strong) NSString *beforeSting;
@property (nonatomic, strong) NSString *firstString;
@property (nonatomic, strong) UITextField *resultTF;
@property (nonatomic, strong) UITextField *caculatorTF;

@property (nonatomic, assign) BOOL isCaculating;
@property (nonatomic, assign) NSInteger caculateType;

@property (nonatomic, strong) AMapLocationManager *locationManager;

@property (nonatomic, strong) NSMutableArray *homeDataArr;

@property (nonatomic, strong) NSIndexPath *currentIndex;
@property (nonatomic, assign) float currentPrice;
@property (nonatomic, assign) NSInteger scrollCellRow;

@property (nonatomic, strong) NSString *updateDate;

@end

@implementation ERHomeViewController

- (NSMutableArray *)homeDataArr
{
    if (!_homeDataArr)
    {
        _homeDataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _homeDataArr;
}

- (NSIndexPath *)currentIndex
{
    if (!_currentIndex)
    {
        _currentIndex = [[NSIndexPath alloc] init];
    }
    return _currentIndex;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArr = [NSMutableArray arrayWithCapacity:0];
    
    [self addTableViewOnHomeView];
    [self addCaculatorView];

    [self currentGetDataWay];
    _caculatorString2 = @"0";
    
    _rightButton.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    [_rightButton setImage:[UIImage imageNamed:@"iconfont-gengduo"] forState:(UIControlStateNormal)];
    [_rightButton addTarget:self action:@selector(didEnterMapViewAction) forControlEvents:(UIControlEventTouchUpInside)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSelfView) name:@"changeSuperView" object:@"aa"];
 
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeEditCountry:) name:@"selected" object:@"list"];
    [self choiceLangureClea];
  //  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(choiceLangureClea:) name:@"chang" object:@"change"];
}

#pragma mark 数据请求判断
- (void) currentGetDataWay
{
    if ([self currentNetWork])
    {
        [self configureData];
    }
    else
    {
         [self getDataFromDataBase];
         [self getdataFromShowData];
         [self.mainTableView reloadData];
    }
    
}

#pragma mark 进入更多页面
- (void)didEnterMapViewAction
{

    ERToolsMainViewController *toolsVC = [[ERToolsMainViewController alloc] init];
    [self.navigationController pushViewController:toolsVC animated:YES];
}

- (void)addTableViewOnHomeView
{
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, kSCREENWIDTH, kSCREENHEIGHT * 2 / 3 - 70) style:(UITableViewStylePlain)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = (kSCREENHEIGHT * 2 / 3 - 70) / 4 ;
    _mainTableView.scrollEnabled = NO;
    _mainTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [_mainTableView registerClass:[ERHomeTableViewCell class] forCellReuseIdentifier:kReuseIdentifier];
    [self.view addSubview:_mainTableView];
    
}

- (void)addCaculatorView
{
    _caculatorView = [[ERCaculatorView alloc] initWithFrame:CGRectMake(0, kSCREENHEIGHT * 2 / 3, kSCREENWIDTH, kSCREENHEIGHT / 3)];
    for (int i = 0; i < _caculatorView.imageView.subviews.count; i++)
    {
        UIButton *button = _caculatorView.imageView.subviews[i];
        [button addTarget:self action:@selector(didCaculatorButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        if (i == 14)
        {
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPressOnclearButton)];
            [button addGestureRecognizer:longPress];
        }
    }
    [self.view addSubview:_caculatorView];
}


- (void)changeSelfView
{
    [_effectView removeFromSuperview];
    NSArray *arr = [_mainTableView visibleCells];
    [UIView animateWithDuration:0.7 animations:^{
        for (ERHomeTableViewCell *cell in arr)
        {
            cell.scrollView.contentOffset = CGPointMake(kSCREENWIDTH, 0);
        }
    }];
}

- (void)changeEditCountry:(NSNotification *)notification
{
    ERResourceModel *model = notification.userInfo[@"model"];
    ERHomeTableViewCell *cell = [_mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_scrollCellRow inSection:0]];
    [self.homeDataArr removeObjectAtIndex:_scrollCellRow];
    [self.homeDataArr insertObject:model atIndex:_scrollCellRow];
    cell.nationImageView.image = [UIImage imageNamed:model.fields.name];
    cell.nationLabel.text = model.fields.name;
    cell.nationNameLabel.text = model.nameStr;
    if ([_resultString isEqualToString:@""])
    {
        cell.resultTF.placeholder = [NSString stringWithFormat:@"%.2f", 100 / _currentPrice * [model.fields.price floatValue]];
    }
    else
    {
        cell.resultTF.text = [NSString stringWithFormat:@"%.2f", [_resultTF.text floatValue] / _currentPrice * [model.fields.price floatValue]];
    }
}

- (void)didLongPressOnclearButton
{
    _caculatorTF.text = @"";
    _resultTF.text = @"";
    _firstString = @"";
    _caculatorString = @"";
    _caculatorString2 = @"";
    _isCaculating = NO;
    _resultString = @"";
    [_resultTF becomeFirstResponder];
}

// 计算器的实现
- (void)didCaculatorButtonAction:(UIButton *)button
{
    _resultString = _resultTF.text;
    switch (button.tag - 100)
    {
        case 0:
            [self addNumber:@"7"];
            break;
        case 1:
            [self addNumber:@"8"];
            break;
        case 2:
            [self addNumber:@"9"];
            break;
        case 3:
            _caculatorString2 = @"";
            _caculateType = 1;
            _firstString = _resultString;
            _caculatorString = [_caculatorString stringByAppendingString:@"+"];
            [_caculatorTF becomeFirstResponder];
            _isCaculating = YES;
            break;
        case 4:
            [self addNumber:@"4"];
            break;
        case 5:
            [self addNumber:@"5"];
            break;
        case 6:
            [self addNumber:@"6"];
            break;
        case 7:
            _caculateType = 2;
            _caculatorString2 = @"";
            _firstString = _resultString;
            _caculatorString = [_caculatorString stringByAppendingString:@"-"];
            [_caculatorTF becomeFirstResponder];
            _isCaculating = YES;
  
            break;
        case 8:
            [self addNumber:@"1"];

            break;
        case 9:
            [self addNumber:@"2"];

            break;
        case 10:
            [self addNumber:@"3"];
            break;
        case 11:
            _caculateType = 3;
            _caculatorString2 = @"";
            if ([_caculatorString containsString:@"+"] || [_caculatorString containsString:@"-"]) {
                _caculatorString = [NSString stringWithFormat:@"(%@)", _caculatorString];
            }
            _firstString = _resultString;
            _caculatorString = [_caculatorString stringByAppendingString:@"×"];
            [_caculatorTF becomeFirstResponder];
            _isCaculating = YES;
            break;
        case 12:
            [self addNumber:@"."];
            break;
        case 13:
            [self addNumber:@"0"];
            break;
        case 14:
        {
            if (!_isCaculating)
            {
                if (_resultString.length > 0)
                {
                    _resultString = [_resultString substringWithRange:NSMakeRange(0, _resultString.length - 1)];
                    [self refreshCellAboutResult];
                }
                else
                {
                    [self refreshCellAboutResultTFPlacehold];
                }
                _resultTF.text = _resultString;
            }
            else
            {
                if (_caculatorString.length > 0 && [_caculatorString containsString:@"+"] && [_caculatorString containsString:@"-"] && [_caculatorString containsString:@"×"] && [_caculatorString containsString:@"÷"])
                {
                    _caculatorString = [_caculatorString substringWithRange:NSMakeRange(0, _caculatorString.length - 1)];
                }
                else
                {
                    [_resultTF becomeFirstResponder];
                    _isCaculating = NO;
                    _caculatorString2 = @"";
                    _caculatorString = @"";
                    _beforeSting = @"";
                    
                }
                _caculatorTF.text = _caculatorString;
            }
        }
            break;
        case 15:
            _caculateType = 4;
            _caculatorString2 = @"";
            if ([_caculatorString containsString:@"+"] || [_caculatorString containsString:@"-"]) {
                _caculatorString = [NSString stringWithFormat:@"(%@)", _caculatorString];
            }
            _firstString = _resultString;
            _caculatorString = [_caculatorString stringByAppendingString:@"÷"];
            [_caculatorTF becomeFirstResponder];
            _isCaculating = YES;
     
            break;
        default:
            break;
    }
    if (!_isCaculating)
    {
        _resultTF.text = _resultString;
        _caculatorString = _resultString;
    }
    else
    {
        _caculatorTF.text = _caculatorString;
    }
}

- (void)addNumber:(NSString *)num
{
    if (!_isCaculating)
    {
        _resultString = [_resultString stringByAppendingString:num];
        [self refreshCellAboutResult];

    }
    else
    {
        _caculatorString2 = [_caculatorString2 stringByAppendingString:num];
        _caculatorString = [_caculatorString stringByAppendingString:num];
        
        switch (_caculateType)
        {
            case 1:
            {
                _resultTF.text = [NSString stringWithFormat:@"%.2f", [_firstString floatValue] + [_caculatorString2 floatValue]];
                _resultString = _resultTF.text;
                [self refreshCellAboutResult];
            }
                break;
            case 2:
            {
                _resultTF.text = [NSString stringWithFormat:@"%.2f", [_firstString floatValue] - [_caculatorString2 floatValue]];
                _resultString = _resultTF.text;
                [self refreshCellAboutResult];
            }
                break;
            case 3:
            {
                
                _resultTF.text = [NSString stringWithFormat:@"%.2f", [_firstString floatValue] * [_caculatorString2 floatValue]];
                _resultString = _resultTF.text;
                [self refreshCellAboutResult];
            }
                break;
            case 4:
            {
                
                _resultTF.text = [NSString stringWithFormat:@"%.2f", [_firstString floatValue] / [_caculatorString2 floatValue]];
                _resultString = _resultTF.text;
                [self refreshCellAboutResult];
            }
                break;
            default:
                break;
        }
        
    }
}

- (void)refreshCellAboutResult
{
    NSArray *arr =[_mainTableView visibleCells];
    for (int i = 0; i < arr.count; i++)
    {
        ERHomeTableViewCell *cell = (ERHomeTableViewCell *)arr[i];
        ERResourceModel *model = self.homeDataArr[i];
        if ([_resultTF isEqual:cell.resultTF])
        {
            NSLog(@"enter");
        }
        else
        {
            
            NSLog(@"%@---%d--%.2f, %@", model.fields.name, i, _currentPrice, _resultString);
            cell.resultTF.text = [NSString stringWithFormat:@"%.2f", [_resultString floatValue] / _currentPrice * [model.fields.price floatValue]];
        }
    }
}

- (void)refreshCellAboutResultTFPlacehold
{
    NSArray *arr =[_mainTableView visibleCells];
    for (int i = 0; i < arr.count; i++)
    {
        ERHomeTableViewCell *cell = (ERHomeTableViewCell *)arr[i];
        ERResourceModel *model = self.homeDataArr[i];
        cell.resultTF.text = @"";
        if ([_resultTF isEqual:cell.resultTF])
        {
            NSLog(@"enter");
        }
        else
        {
            cell.resultTF.placeholder = [NSString stringWithFormat:@"%.2f", 100 / _currentPrice * [model.fields.price floatValue]];
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.homeDataArr == nil)
    {
        return 4;
    }
    return self.homeDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ERHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseIdentifier forIndexPath:indexPath];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnCell:)];
    cell.scrollView.delegate = self;
    ERResourceModel *model = self.homeDataArr[indexPath.row];
    cell.configureModel = model;
    [cell addGestureRecognizer:tap];
    if (indexPath.row == 0)
    {
        cell.scrollView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        cell.resultTF.userInteractionEnabled = YES;
        cell.caculatorTF.userInteractionEnabled = YES;
        cell.resultTF.placeholder = @"100";
        _resultTF = cell.resultTF;
        _caculatorTF = cell.caculatorTF;
        _resultString = @"";
        self.currentIndex = indexPath;
        _currentPrice = [((ERResourceModel *)self.homeDataArr[0]).fields.price floatValue];
    }
    else
    {
        cell.resultTF.placeholder = [NSString stringWithFormat:@"%.2f", 100 / _currentPrice * [model.fields.price floatValue]];
    }
    [cell.resultTF becomeFirstResponder];
    
    return cell;
}

- (void)didTapOnCell:(UITapGestureRecognizer *)tap
{
    _resultString = @"";
    _caculatorString = @"";
    _caculatorString2 = @"";
    _beforeSting = @"";
    _isCaculating = NO;
    ERHomeTableViewCell *cell = (ERHomeTableViewCell *)tap.view;
    for (ERHomeTableViewCell *aCell in [_mainTableView visibleCells])
    {
        aCell.scrollView.backgroundColor = [UIColor whiteColor];
        aCell.resultTF.userInteractionEnabled = NO;
    }
    cell.resultTF.userInteractionEnabled = YES;
    self.currentIndex = [_mainTableView indexPathForCell:cell];
    _currentPrice = [((ERResourceModel *)self.homeDataArr[_currentIndex.row]).fields.price floatValue];
    NSLog(@"aaaaaaaa----%ld", _currentIndex.row);
    if (!_isCaculating)
    {
        
        [cell.resultTF becomeFirstResponder];
        cell.resultTF.placeholder = @"100";
        if ([_resultString isEqualToString:@""])
        {
            NSArray *arr =[_mainTableView visibleCells];
            for (int i = 0; i < arr.count; i++)
            {
                ERHomeTableViewCell *cell = (ERHomeTableViewCell *)arr[i];
                cell.resultTF.text = @"";
                ERResourceModel *model = self.homeDataArr[i];
                if ([_mainTableView indexPathForCell:cell].row == _currentIndex.row)
                {
                    NSLog(@"enter");
                }
                else
                {
                    cell.resultTF.placeholder = [NSString stringWithFormat:@"%.2f", 100 / _currentPrice * [model.fields.price floatValue]];
                }
            }
        }
        
    }
    else
    {
        NSLog(@"没有正在编辑");
    }
    cell.scrollView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    _resultTF = cell.resultTF;
    _caculatorTF = cell.caculatorTF;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x <= kSCREENWIDTH - 150)
    {
        for (ERHomeTableViewCell *cell in [_mainTableView visibleCells])
        {
            if ([cell.scrollView isEqual:scrollView])
            {
                _scrollCellRow = [_mainTableView indexPathForCell:cell].row;
            }
        }
        _countryListVC = [[ERCountryListViewController alloc] init];
        [self addChildViewController:_countryListVC];
        _dataCellArr = [self changeEditStye:_dataCellArr andValue:1 andSimpleName:((ERResourceModel *)self.homeDataArr[_scrollCellRow]).fields.name];
        _countryListVC.editName = ((ERResourceModel *)self.homeDataArr[_scrollCellRow]).fields.name;
        [self assignmentForListVC];
        CATransition *animation = [CATransition animation];
        animation.duration = 0.3;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.type = @"push";
        animation.subtype = kCATransitionFromRight;
        
        [self.view.window.layer addAnimation:animation forKey:nil];
//        [self.view addSubview:_countryListVC.view];
        [self addEffectViewToCountryView];
        scrollView.contentOffset = CGPointMake(0, 0);
    }
}

#pragma mark 增加模糊效果
- (void)addEffectViewToCountryView
{
    // 创建模糊View
    _effectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)]];
    // 设定模糊尺寸
    _effectView.frame = self.view.bounds;
    // 添加到View中
    [self.view addSubview:_effectView];

    [_effectView.contentView addSubview:_countryListVC.view];
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSLog(@"%@", title);
    return index;
}

#pragma mark 数据请求
- (void) configureData
{
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&"]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.dataArr removeAllObjects];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        [self cleanDataInDataBase];
        NSArray *array = dict[@"list"][@"resources"];
        BOOL isHave = [self isHaveDataInERShowData];
        for (NSDictionary *modelDic in array)
        {
            NSDictionary *resourceDic = modelDic[@"resource"];
            ERResourceModel *model = [[ERResourceModel alloc] init];
            [model setValuesForKeysWithDictionary:resourceDic];
            if ([model.fields.name containsString:@"USD"] && model.fields.name.length > 3) {
                NSString *name = [model.fields.name substringFromIndex:4];
                model.fields.name = name;
            }
            if (isHave) {
                if ([model.fields.name isEqualToString:@"USD"] || [model.fields.name isEqualToString:@"EUR"] || [model.fields.name isEqualToString:@"JPY"] || [model.fields.name isEqualToString:@"CNY"])
                {
                    model.fields.isSelected = 1;
                    [self saveShowCountryModelInDataBase:model];
                }
            }
            model.fields.isLocation = 0;
            //model.fields.isSelected = 0;
            [self saveDataInDataBase:model];
            [_dataArr addObject:model];
            
        }
        [self analysisForData];
        [self getdataFromShowData];
        [_mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
}

#pragma mark 判断ERShowData 当中是否存在数据
- (BOOL) isHaveDataInERShowData
{
    FMResultSet *result= [self.dataBase executeQueryWithFormat:@"select *from ERShowData"];
    while ([result next]) {
        return NO;
    }
    return YES;
}

#pragma mark 存入数据库
- (void) saveDataInDataBase:(ERResourceModel *)model
{
    [self.dataBase executeUpdate:@"insert into ERData values( ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.fields.name, model.fields.price, model.fields.symbol, model.fields.ts, model.fields.type, model.fields.utctime, model.fields.volume, [NSString stringWithFormat:@"%d", model.fields.isSelected], [NSString stringWithFormat:@"%d", model.fields.isLocation]
     ];
}

#pragma mark 将显示的存入数据库
- (void) saveShowCountryModelInDataBase:(ERResourceModel *)model
{
    [self.dataBase executeUpdate:@"insert into ERShowData values( ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.fields.name, model.fields.price, model.fields.symbol, model.fields.ts, model.fields.type, model.fields.utctime, model.fields.volume, [NSString stringWithFormat:@"%d", model.fields.isSelected], [NSString stringWithFormat:@"%d", model.fields.isLocation]];
}

#pragma mark 清除数据库中所有数据
- (void) cleanDataInDataBase
{
    [self.dataBase executeUpdate:@"delete from ERData"];
}

#pragma mark 从数据库中取值
- (void) getDataFromDataBase
{
    FMResultSet *result= [self.dataBase executeQueryWithFormat:@"select *from ERData"];
    [_dataArr removeAllObjects];
    _dataArr = [self fromDataBaseToModel:result];
    NSString *timeStr = [[_updateDate stringByReplacingOccurrencesOfString:@"T" withString:@" "] substringWithRange:NSMakeRange(0, 19)];
    [self topAlertShowTitle:NSLocalizedString(@"当前无网络", nil) andMessage:[NSString stringWithFormat:NSLocalizedString(@"当前数据为%@更新的数据", nil), timeStr] andType:(TWMessageBarMessageTypeInfo)];
    [self analysisForData];
}

#pragma mark 从显示的数据库中取值
- (void) getdataFromShowData
{
    [_homeDataArr removeAllObjects];
    FMResultSet *result= [self.dataBase executeQueryWithFormat:@"select *from ERShowData"];
    _homeDataArr = [NSMutableArray arrayWithArray:[self fromDataBaseToModel:result]];
    _homeDataArr = [self addCurrentName:_homeDataArr];
    [self getShowSimName];
}

#pragma mark 获取显示数据简称
- (void) getShowSimName
{
    _dataShowSimArr = [NSMutableArray arrayWithCapacity:0];
    [_dataShowSimArr removeAllObjects];
    for (ERResourceModel *model in _homeDataArr) {
        NSString *str = model.fields.symbol;
        NSString *name = [str substringWithRange:NSMakeRange(0, str.length - 2)];
        [_dataShowSimArr addObject:name];
    }
}

#pragma mark 添加名称
- (NSMutableArray *) addCurrentName:(NSMutableArray *)array
{
    for (ERResourceModel *model in array) {
        for (NSDictionary *dic in self.jsonDataArr) {
            if ([model.fields.symbol containsString:dic[@"symbol"]]) {
                model.nameStr = [dic objectForKey:[NSString stringWithFormat:@"currency_name_%@", self.counSim]];
            }
        }
    }
    return array;
}

#pragma mark 从数据库中取完之后放进数组中
- (NSMutableArray *) fromDataBaseToModel:(FMResultSet *)result
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    while ([result next]) {
        ERResourceModel *model = [[ERResourceModel alloc] init];
        NSString *name = [result stringForColumn:@"name"];
        NSString *price = [result stringForColumn:@"price"];
        NSString *symbol = [result stringForColumn:@"symbol"];
        NSString *ts = [result stringForColumn:@"ts"];
        NSString *type = [result stringForColumn:@"type"];
        NSString *utctime = [result stringForColumn:@"utctime"];
        NSString *volume = [result stringForColumn:@"volume"];
        int isSelected = [[result stringForColumn:@"isSelected"] intValue];
        int isLocation = [[result stringForColumn:@"isLocation"] intValue];
        model.fields.name = name;
        model.fields.price = price;
        model.fields.symbol = symbol;
        model.fields.ts = ts;
        model.fields.type = type;
        model.fields.utctime = utctime;
        model.fields.volume = volume;
        model.fields.isSelected = isSelected;
        model.fields.isLocation = isLocation;
        _updateDate = model.fields.utctime;
        [array addObject:model];
    }
    return array;
}

#pragma mark 解析数据
- (void) analysisForData
{
    _dataCellArr = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *allArr = [NSMutableArray arrayWithCapacity:0];
    for (NSString *section in self.dataSectionArr) {
        NSMutableArray *smallArr = [NSMutableArray arrayWithCapacity:0];
        NSArray *everyArr = self.allSimDic[section];
        for (int i = 0; i < everyArr.count; i ++) {
            for (ERResourceModel *model in _dataArr) {
                if ([model.fields.symbol containsString:everyArr[i]]) {
                    model.nameStr = self.allNameDic[section][i];
                    model.isEdit = 0;
                    [smallArr addObject:model];
                }
            }
        }
        [allArr addObject:smallArr];
    }
    _dataCellArr = allArr;
}

#pragma mark 给List赋值
- (void) assignmentForListVC
{
    _countryListVC.dataCellArr = _dataCellArr;
    _countryListVC.dataTitleArr = self.dataSectionArr;
    _countryListVC.dataNameCounArr = self.dataCounNameArr;
    _countryListVC.dataNameSimArr = self.dataSimCounArr;
    _countryListVC.nameDic = self.allNameDic;
    _countryListVC.simDic = self.allSimDic;
    _countryListVC.dataShowSimArr = _dataShowSimArr;
    [_countryListVC.listTV.tableView reloadDataAnimateWithWave:(LeftToRightWaveAnimation)];
}

#pragma mark 接收选择语言消息
- (void) choiceLangureClea
{
    __weak ERHomeViewController *test = self;
    test.reload = ^(void)
    {
        [test getdataFromShowData];
        [test.mainTableView reloadData];
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
